package com.pn.service.impl;

import com.pn.entity.InStore;
import com.pn.entity.Product;
import com.pn.entity.Purchase;
import com.pn.entity.Result;
import com.pn.mapper.ProductMapper;
import com.pn.mapper.PurchaseMapper;
import com.pn.page.Page;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.pn.mapper.InStoreMapper;
import com.pn.service.InStoreService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class InStoreServiceImpl implements InStoreService {

    @Resource
    private InStoreMapper inStoreMapper;
    @Resource
    private PurchaseMapper purchaseMapper;
    @Resource
    private ProductMapper productMapper;

    @Transactional
    @Override
    public Result addInStore(InStore inStore, Integer bid) {
        int i = inStoreMapper.insertInStore(inStore);
        if (i > 0) {
            int result = purchaseMapper.updateIsInStateBid(bid);
            if (result > 0) {
                return Result.ok("入库单记录插入成功！");
            }
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "入库单记录插入失败！");
    }

    @Override
    public Page findInStoreByPage(InStore inStore, Page page) {
        int result = inStoreMapper.countInStore(inStore);
        page.setTotalNum(result);
        List<InStore> inStores = inStoreMapper.selectInStoreByPage(inStore, page);
        page.setResultList(inStores);
        return page;
    }

    @Transactional
    @Override
    public Result modifyIsIn(Integer insId, Integer inNum, Integer pid,Integer updateBy) {
        int result = inStoreMapper.updateIsInByInsId(insId);
        if (result > 0) {
            result = productMapper.updateInventByPid(pid, inNum,updateBy);
            if (result > 0) {
                return Result.ok("确认入库成功！");
            }
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "确认入库失败！");
    }






}
